package cn.sharp.bbs.controller;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.dao.PublishArticleDao;
import cn.sharp.bbs.dao.gen.*;
import cn.sharp.bbs.entity.BO.*;
import cn.sharp.bbs.entity.DO.*;
import cn.sharp.bbs.event.PostCreateEvent;
import cn.sharp.bbs.service.CommonService;
import cn.sharp.bbs.service.PostService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 发帖
 * @author 杨择润
 * @create 2022-07-09 11:09
 */
@RestController
@RequestMapping("/post")
public class PostController{
    @Resource
    PostService postService;
    @Resource
    CommonService commonService;
    @Resource
    LabelMapper labelMapper;
    @Resource
    PostlabelMapper postlabelMapper;
    @Resource
    PostMapper postMapper;
    @Resource
    PostversionMapper postversionMapper;
    @Resource
    PublishArticleDao publishArticleDao;
    @Resource
    CommentMapper commentMapper;






    @PostMapping("/publish")
    public Result publishArticle(@RequestAttribute("CurrentUser") CurrentUser user, @RequestBody PublishArticle publishArticle){

        publishArticle.setUserId(user.getU_id());
        PublishArticle a=postService.publishArticle(publishArticle);
        return Result.success( a);
    }


    @ResponseBody
    @PostMapping("/uploadImg")
    //RequestParam中的属性名称要和前端定义的一致．所以写"img"
    //使用List<MultipartFile>进行接收
    //返回的是一个Ｄto类，后面会说明，使用@ResponseBody会将其转换为Json格式数据
    // 上传图片先保存到用户id下，当点击发布文章时，再把图片中的文章id更新，主要是在编辑文章时不知道正在编辑的文章的id
    public Result uploadEditorImg(@RequestParam("img") List<MultipartFile> list, ImgResultDto imgResultDto, Img imgDto) {
        //服务曾处理数据，返回Dto
        System.out.println(imgDto);
        try{
            postService.uploadImg(imgDto, imgResultDto, list);
            return Result.success("上传成功", imgResultDto);
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_BUSINESS, "上传失败！");
        }
    }

    @ResponseBody
    @PostMapping("/selectalltabs")
    public Result getalltabs(){
        List<Label> a=labelMapper.selectAll();
        return Result.success(a);
    }

    //给文章附加标签
    @PostMapping("/addtabs")
    public Result addTabs(@RequestBody PostTabs postTabs){
        Postlabel postlabel=new Postlabel();
        int[] arr=postTabs.getTabs();
        postlabel.setPostId(postTabs.getPostId());
        System.out.println("posttabs"+postTabs);
        if(arr==null){
            return Result.success();
        }
        for(int i=0;i<arr.length;i++){
            long num=arr[i];
            postlabel.setLabelId(num);
            postlabelMapper.insert(postlabel);
        }
        return Result.success();
    }
    //新建标签
    @PostMapping("/newtabs")
    public Result createtabs(@RequestAttribute("CurrentUser") CurrentUser user, @RequestBody Label label){
        label.setUserId(user.getU_id());
        //获取当前时间值
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        label.setTime(date);
        labelMapper.insert(label);
        return Result.success();
    }
    //查找历史文章
    @PostMapping("/getPublishedAriticle")
    public Result getPublishedAriticle(@RequestBody PublishArticle publishArticle){
        //空值时返回正确
        if(publishArticle.getPostId()==null){
            return Result.success();
        }
        Long postid=publishArticle.getPostId();


        //写在最前面，防止被覆盖

        Post post=postMapper.selectByPrimaryKey(publishArticle.getPostId());
        System.out.println(post);

        BeanUtils.copyProperties(post,publishArticle);


        System.out.println(postid);
        Postversion postversion=publishArticleDao.searchArticleByid(postid);
        System.out.println(postversion);
        BeanUtils.copyProperties(postversion,publishArticle);
        publishArticle.setA_html(postversion.getCont());
        publishArticle.setA_title(postversion.getTitle());
        return Result.success(publishArticle);
    }
    //进行修改
    @PostMapping("/republish")
    public Result republishArticle(@RequestAttribute("CurrentUser") CurrentUser user, @RequestBody PublishArticle publishArticle){
        publishArticle.setUserId(user.getU_id());
        PublishArticle a=postService.republishArticle(publishArticle);
        return Result.success( a);
    }
    //把帖子状态设置为删除
    @PostMapping("/deletearticle")
    public Result deletearticle(@RequestBody Postversion postversion){
        publishArticleDao.deleteArticle(postversion);
        return Result.success();
    }
    //关键词查询
    @PostMapping("/selectarticlebyword")
    public Result selectarticlebyword(@RequestAttribute("CurrentUser") CurrentUser user,@RequestBody SearchQuery searchQuery){

        List<PostDto> publishArticles=postService.selectArticle(user.getU_id(),searchQuery.getS());
        System.out.println("s1"+searchQuery.getS());
        return Result.success(publishArticles);
    }
    //通过标签查询帖子
    @PostMapping("/selectarticlebytabs")
    public Result selectarticlebytabs(@RequestBody int[] arr){
        System.out.println(arr.length);
        List<Postversion> publishArticles=publishArticleDao.selectArticleTabs(arr);
        return Result.success(publishArticles);
    }
    //获取文章列表，纯文本内容
    @PostMapping("/getpublishlist")
    public Result getpublishlist(){
        List<Postversion> a=postversionMapper.selectAll();
        for(Postversion postversion: a){
            postversion.setCont(postversion.getCont());
        }
        System.out.println(a);
        return Result.success(a);
    }
    //聚合搜索
    @PostMapping("/selectarticlebyall")
    public Result selectarticlebyall(@RequestBody SearchArticle searchArticle){
        System.out.println(searchArticle);
        List<Postversion> publishArticles=publishArticleDao.selectArticleAll(searchArticle);
        return Result.success(publishArticles);
    }
    //富文本转纯文本
    public static String getText(String richText) {
        String regx = "(<.+?>)|(</.+?>)";
        Matcher matcher = Pattern.compile(regx).matcher(richText);
        while (matcher.find()) {
            // 替换图片
            richText = matcher.replaceAll("").replace(" ", "");
        }
        return richText;
    }

    @PostMapping("/selectuserarticle")
    public Result selectUserarticlebyword(@RequestAttribute("CurrentUser") CurrentUser user){

        List<PostDto> publishArticles=postService.selectUserArticle(user.getU_id(),"");

        return Result.success(publishArticles);
    }

    @PostMapping("/selectarticlebypostid")
    public Result selectarticlebypostid( @RequestBody PostDto postDto){

        PostDto post=postService.selectArticleBypostid(postDto.getPostId());

        return Result.success(post);
    }

    @PostMapping("/getarticlecomment")
    public Result getarticlecomment( @RequestBody Post post){

        List<CommentDto> comments=postService.getArticlecomment(post.getId());

        return Result.success(comments);
    }

//    @PostMapping("/goodarticle")
//    public Result goodarticle( @RequestBody Post post){
//
//        publishArticleDao.goodartilce(post.getId());
//
//        return Result.success();
//    }
//
//    @PostMapping("/badarticle")
//    public Result badarticle( @RequestBody Post post){
//
//        publishArticleDao.badartilce(post.getId());
//
//        return Result.success();
//    }
//
    @PostMapping("/deletetabs")
    public Result deletetabs( @RequestBody PostTabs postTabs){

        int[] arr=postTabs.getTabs();
        System.out.println(postTabs);
        if(arr==null){
            System.out.println("1111111");
            return Result.success();

        }
        for(int i=0;i<arr.length;i++){
            long num=arr[i];
            labelMapper.deleteByPrimaryKey(num);
        }

        return Result.success();
    }
//
@PostMapping("/createcomment")
public Result createcomment( @RequestAttribute("CurrentUser") CurrentUser user,@RequestBody Comment comment){
    comment.setUserId(user.getU_id());
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    comment.setTime(date);
    commentMapper.insert(comment);
    return Result.success();
}




}
